//
//  LXLViewController.swift
//  Weibo-YQY-test
//
//  Created by Lang on 10/9/15.
//  Copyright © 2015 Lang. All rights reserved.
//

import UIKit

class LXLTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let tab = LXLTabBar()
        
        tab.composeButtonClickBlock = {
            print("点击的闭包")
        }
        
        setValue(tab, forKey: "tabBar")
        

        tabBar.tintColor = UIColor.orangeColor()
        // 添加子控制器
        addChildViewControllers()
    }

    private func addChildViewControllers() {
        
        
        addChildViewController(LXLHomeViewController(), title: "首页", imageName: "tabbar_home", index: 0)
        addChildViewController(LXLMessageViewController(), title: "消息", imageName: "tabbar_message_center", index: 1)
        addChildViewController(LXLDiscoverViewController(), title: "发现", imageName: "tabbar_discover", index: 2)
        addChildViewController(LXLProfileViewController(), title: "我", imageName: "tabbar_profile", index: 3)
    }
    
    private func addChildViewController(childViewController: UIViewController, title: String, imageName: String, index: Int) {
        
        
        // 使用自定义的 tabBarItem
        let item = LXLTabBarItem()
        
        item.index = index
        
        childViewController.tabBarItem = item
        
        
        
//        childViewController.tabBarItem.title = title
        // 设置 controller 的title 不仅可以设置 tabBarItem 的title， 还可以设置 navigationItem 的 title
        childViewController.title = title
        
        
        
        childViewController.tabBarItem.image = UIImage(named: imageName)
        childViewController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")
        
        let nav = LXLNavigationController(rootViewController: childViewController)
        
        addChildViewController(nav)
    }

    //tabBarItem的点击事件
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        
        printLog("\(item.title)")
        
//        
        let lxlItem = item as! LXLTabBarItem
        //记录当前遍历的 uitabBarButton 是第几个
        var index = 0
        for tabBarChild in tabBar.subviews {
            if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                if index == lxlItem.index {
                    for tabBarButtonChild in tabBarChild.subviews{
                        if tabBarButtonChild.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
                            //拿到显示 image 的控件
                            tabBarButtonChild.transform = CGAffineTransformMakeScale(0.8, 0.8)
                            
                            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                                
                                tabBarButtonChild.transform = CGAffineTransformIdentity
                                
                                }, completion: { (finish) -> Void in
                                    
                            })
                        }
                    }
                }
                
                index++
                
            }
        }
    }
    
    
}
